package chai.ningning.tcp;

        import java.io.*;
        import java.net.InetSocketAddress;
        import java.net.Socket;
        import java.net.SocketAddress;
        import java.nio.charset.Charset;
        import java.util.Scanner;

public class SecondClient {

    private static final Charset DEFAULT_CHARSET = Charset.forName( "UTF-8" );

    static class ReceiveThread extends Thread {

        private Socket socket ; // 用来保存当前的客户端对应的Socket对象

        public ReceiveThread(Socket socket) {
            super( "Receive" );
            this.socket = socket;
        }

        @Override
        public void run() {
            try {
                InputStream in = socket.getInputStream(); // 字节输入流
                Reader reader = new InputStreamReader(in, DEFAULT_CHARSET); // 字符输入流
                BufferedReader br = new BufferedReader( reader ); // 根据字符输入流创建缓冲流(仍然是字符流)
                String s ;
                while( ( s = br.readLine() ) != null ){
                    System.out.println( s );
                }
            } catch ( IOException e ) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) throws IOException {

        final Scanner scanner = new Scanner( System.in );

        final String remoteHost = "1.0.0.45" ;// 远程主机的IP地址
        final int remotePort = 55555 ; // 服务端程序的端口号

        System.out.println( "创建客户端程序..." );
        Socket client = new Socket();

        SocketAddress remote = new InetSocketAddress( remoteHost , remotePort );
        System.out.println( "连接服务器程序..." );
        client.connect( remote );
        System.out.println( client );

        System.out.println( "创建并启动接收数据的线程..." );
        Thread t = new ReceiveThread( client );
        t.start();

        OutputStream out = client.getOutputStream() ;
        PrintStream ps = new PrintStream( out );

        String s ;
        while( ( s = scanner.nextLine() ) != null ) {
            ps.println( s );
        }

    }

}
/**
    我刘博轩敢上仙人掌
    我刘博轩敢上仙人掌
    我刘博轩敢上仙人掌
    我刘博轩敢上仙人掌
    我刘博轩敢上仙人掌
    我刘博轩敢上仙人掌
    我刘博轩敢上仙人掌
    我刘博轩敢上仙人掌
    我刘博轩敢上仙人掌
    我刘博轩敢上仙人掌
    我刘博轩敢上仙人掌

            /**我刘天阳终究是个傻逼，只喜欢赋值粘贴霸屏
        我刘天阳终究是个傻逼，只喜欢赋值粘贴霸屏
        我刘天阳终究是个傻逼，只喜欢赋值粘贴霸屏
        我刘天阳终究是个傻逼，只喜欢赋值粘贴霸屏
        我刘天阳终究是个傻逼，只喜欢赋值粘贴霸屏
        我刘天阳终究是个傻逼，只喜欢赋值粘贴霸屏
        我刘天阳终究是个傻逼，只喜欢赋值粘贴霸屏
        我刘天阳终究是个傻逼，只喜欢赋值粘贴霸屏
        我刘天阳终究是个傻逼，只喜欢赋值粘贴霸屏
             */